/* 
MIP Time Series 
Data are from the LIBRES surveys.
Data organized and analyzed by the authors: Amnon Cavari and Asif Efrat 
*/

cd "~/dropbox/AmnonDocuments/Projects/MIP_Israel/Polarized Agenda Crisis JEPP"


* Google Trends Democracy
{
use "Google Trends Democracy.dta", clear 

tsline democracy government seperation, ///
    lwidth(thick thick thick) lpattern(solid shortdash longdash) ///
    xline(23014, lpattern(dash) lcolor(gs8)) xtitle("") ///
    text(110 22930 "Constitutional Overhaul", place(e)) ///
    ytitle("Popularity of Searches") ///
    yscale(range(0 120)) ylabel(0(20)100) ///
    legend(label(1 "Democracy") label(2 "Government") label(3 "Separation of Powers") rows(1))
graph export "Figure1_GoogleTrends.png", width(4800) height(3600) replace
}

* POLITICAL TRUST
{
use "Trust_Overall.dta", clear

sum trust_government	
twoway ///
    (line trust_government MONTHLY, lcolor(gray) lpattern(solid) lwidth(thick)) ///
    (line trust_government1 MONTHLY, lcolor(red) lpattern(longdash) lwidth(thick)) ///
    (line trust_government2 MONTHLY, lcolor(blue) lpattern(dash_dot) lwidth(thick)), ///
    ylabel(0(.2)1) yline(0.5, lpattern(dot) lcolor(gs8)) ///
    ytitle("Mean Trust") ///
    xtitle("") xline(756 765, lpattern(dash) lcolor(gs8)) ///
    text(0.95 750 "Constitutional Overhaul", place(e)) ///
    text(0.95 763 "October 7", place(e)) ///
    title("") ///
    caption("Data collected by authors", justification(right)) ///
    legend(order(1 "All" 2 "Anti-Netanyahu" 3 "Pro-Netanyahu") rows(1) position(6))
graph export "Figure2_Trust.png", width(4800) height(3600) replace
} 



* ANALYIS OF MIP IN CRISIS
{
** DESCRIPTIVES
use MIP_ISRAEL_CRISIS_FULL.dta, clear
fre MONTHLY
asdoc tab MONTHLY
recode majorCAP (1/23=1)(else=0), gen(POLICY)
tab MONTHLY POLICY
asdoc tab MONTHLY POLICY
asdoc tab MONTHLY 

// for appendix, showing the representative nature of the surveys
collapse jewish gender ///
	AGE_1 AGE_2 AGE_3 ///
	EDUCATION3_1 EDUCATION3_2 EDUCATION3_3 ///
	RELIG_1 RELIG_2 RELIG_3 RELIG_4 ///
	ANTI_NETANYAHU PRO_NETANYAHU, by(MONTHLY)

putdocx clear	
putdocx begin

putdocx table summary = data(MONTHLY jewish gender ///
	AGE_1 AGE_2 AGE_3  ///
	EDUCATION3_1 EDUCATION3_2 EDUCATION3_3 ///
	RELIG_1 RELIG_2 RELIG_3 RELIG_4 ///
	ANTI_NETANYAHU PRO_NETANYAHU), varnames

putdocx save summary_table, replace	


// overall attention
use MIP_ISRAEL_CRISIS_MONTHLY_LONG.dta, clear
collapse (mean) CAP, by(majorCAP)
graph hbar CAP, over(majorCAP, sort(CAP) descending) ///
	ytitle(Percent)

// overall by period 
use MIP_ISRAEL_CRISIS_MONTHLY_LONG.dta, clear
collapse (mean) CAP, by(PERIOD majorCAP)
graph hbar CAP, over(majorCAP, sort(CAP) descending label(labsize(tiny))) over(PERIOD, label(labsize(small) angle(90))) ///
	ytitle(Percent)
graph hbar CAP if CAP>=1, over(majorCAP, sort(CAP) descending label(labsize(small))) over(PERIOD, label(labsize(small) angle(90))) ///
	ytitle(Percent)
graph export "Figure3_PublicAgenda.png", width(4800) height(3600) replace

// calculate Shannon H 
use MIP_ISRAEL_CRISIS_MONTHLY_WIDE.dta, clear 
reshape long CAP, i(PERIOD MONTHLY) j(majorCAP) 
collapse (mean) CAP, by(PERIOD majorCAP)

bysort PERIOD (majorCAP): gen total = sum(CAP)
bysort PERIOD (majorCAP): replace total = total[_N]

gen prop = CAP / total

gen shannon_component = -prop * ln(prop)

bysort PERIOD (majorCAP): gen H = sum(shannon_component)
bysort PERIOD (majorCAP): replace H = H[_N]

egen tag = tag(PERIOD majorCAP)
egen n_categories = total(tag), by(PERIOD)

gen H_norm = H / ln(n_categories)


// graph by time
use MIP_ISRAEL_CRISIS_MONTHLY_WIDE.dta, clear 
gen CAP2023=CAP20+CAP23
gen CAP1619=CAP16+CAP19
twoway line CAP2023 CAP1619 MONTHLY, ///
	lwidth(thick thick) lpattern(solid longdash) ///
	ylabel(0 20 40 60 80 100) ///
	ytitle(Percent Attention) ///
	xtitle("") xline(756 765) ///
	text(100 752 "Constitutional Overhaul", place(e)) ///
    text(100 763 "October 7", place(e)) ///
	legend(label(1 "Government") label(2 "Security") position(6) rows(1)) ///
	note("Government includes government (20) and culture/identity (23)" ///
		"Security includes defense (16) and foreign relations (19)")
graph export "Figure4_CrisesTopics.png", width(4800) height(3600) replace


// Agenda Overlap
use MIP_ISRAEL_CRISIS_FULL.dta, clear
fre party_bloc
replace party_bloc=1 if party_bloc==0
fre party_bloc

collapse (sum) policy, by(MONTHLY majorCAP party_bloc)
drop if missing(party_bloc)
drop if missing(majorCAP)
bysort MONTHLY party_bloc: egen major_totalresponses=total(policy) 
sort MONTHLY majorCAP
gen CAP=100*policy/major_totalresponses
collapse (mean) CAP, by(party_bloc majorCAP)
reshape wide CAP, i(majorCAP) j(party_bloc)
replace CAP1=0 if missing(CAP1)
reshape long CAP, i(majorCAP) j(party_bloc)
sort majorCAP
graph hbar CAP, over(majorCAP, sort(CAP) descending label(labsize(tiny))) over(party_bloc, label(labsize(small) angle(90))) ///
	ytitle(Percent)  
graph hbar CAP, ///
	over(party_bloc, label(labsize(tiny) angle(0))) ///
	over(majorCAP, sort(CAP) descending label(labsize(tiny))) ////
	ytitle(Percent)  
reshape wide CAP, i(majorCAP) j(party_bloc)	
egen total_AntiNetanyahu=sum(CAP1)
egen total_ProNetanyahu=sum(CAP2)

gen Difference=abs((100*CAP1/total_AntiNetanyahu)-(100*CAP2/total_ProNetanyahu))
egen TotalDifference=total(Difference)
gen AgendaOverlap=100-TotalDifference	
}


* ANALYSIS
{
cd "~/dropbox/AmnonDocuments/Projects/MIP_Israel/Analysis"
use MIP_ISRAEL_CRISIS_FULL.dta, clear

recode majorCAP (16 = 1)(1/15 17/23 = 0), gen(CAP16)
label variable CAP16 "Attention to Security"
recode majorCAP (16 19 = 1)(1/15 17/18 20/23 = 0), gen(CAP1619)
label variable CAP1619 "Attention to Security and IR"
recode majorCAP (20 = 1)(1/19 21/23 = 0), gen(CAP20)
label variable CAP20 "Attention to Government"
recode majorCAP (20 23 = 1)(1/19 21 = 0), gen(CAP2023)
label variable CAP2023 "Attention to Government and Identity"

label variable CAP2023 "Attention to Government and Identity"
label variable CAP1619 "Attention to Defense and Foreign Affairs"


cibar CAP2023, over(PARTY_BLOC PERIOD) ///
	ciopts(ylabel(0(.2)1) legend(rows(1))) bargap(15) 
graph save CAP2023_PERIOD.gph, replace
cibar CAP16, over(PARTY_BLOC PERIOD) ///
	ciopts(ylabel(0(.2)1) legend(rows(1))) bargap(15)
graph save CAP16_PERIOD.gph, replace
cibar CAP1619, over(PARTY_BLOC PERIOD) ///
	ciopts(ylabel(0(.2)1) legend(rows(1))) bargap(15)
graph save CAP1619_PERIOD.gph, replace
grc1leg CAP2023_PERIOD.gph CAP1619_PERIOD.gph, ///
	legendfrom(CAP1619_PERIOD.gph) 
graph export "Figure5_PartyDifferences.png", width(4800) height(3600) replace


// interactions 
gen interaction1a=CONSTITUTION*ANTI_NETANYAHU
gen interaction2a=SECURITY*ANTI_NETANYAHU
gen interaction2b=ANTI_NETANYAHU*TRUST_GOV
gen interaction2b4=ANTI_NETANYAHU*trust_gov

// Constitutional Crisis
eststo clear
eststo: logit CAP2023 ANTI_NETANYAHU CONSTITUTION ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all, cluster(MONTHLY)
eststo: logit CAP2023 ANTI_NETANYAHU CONSTITUTION interaction1a ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all, cluster(MONTHLY)
esttab


// Security crisis
eststo: logit CAP1619 ANTI_NETANYAHU SECURITY ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all, cluster(MONTHLY)
eststo: logit CAP1619 ANTI_NETANYAHU SECURITY interaction2a ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all, cluster(MONTHLY)
	
esttab est1 est2 est3 est4 using models.rtf, replace se aic bic 

// plotting margins
logit CAP2023 ANTI_NETANYAHU#CONSTITUTION ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all, cluster(MONTHLY)
margins CONSTITUTION#ANTI_NETANYAHU
marginsplot, ///
    title("Constitution Crisis") ///
    xtitle("") xlabel(0 "Pre" 1 "Post") ///
    ytitle("Probability of Attention to Government and Identity") ylabel(0(.2)1) ///
    legend(rows(1)) scheme(s1mono) ///
    plotopts(lwidth(thick) )
graph save margins_constitution.gph, replace 
logit CAP1619 i.ANTI_NETANYAHU##i.SECURITY ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all, cluster(MONTHLY)
margins SECURITY#ANTI_NETANYAHU
marginsplot, ///
	title("Security Crisis") ///
	xtitle("") xlabel(0 "Pre" 1 "Post") ///
	ytitle("Probability of Attention to Security") ylabel(0(.2)1) ///
	legend(rows(1)) scheme(s1mono) ///
    plotopts(lwidth(thick) )
graph save margins_security.gph, replace 
testparm ANTI_NETANYAHU##SECURITY
lincom _b[1.ANTI_NETANYAHU#1.SECURITY]
margins ANTI_NETANYAHU, at(SECURITY=(0 1))

grc1leg margins_constitution.gph margins_security.gph, ycommon
graph export "Figure6_Margins_Major.png", width(4800) height(3600) replace


// trust 2 scale / security crisis 
eststo clear
eststo: logit CAP1619 TRUST_GOV ANTI_NETANYAHU ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all ///
	if SECURITY==1, cluster(MONTHLY)
eststo: logit CAP1619 TRUST_GOV ANTI_NETANYAHU interaction2b ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all ///
	if SECURITY==1, cluster(MONTHLY)
eststo: logit CAP2023 TRUST_GOV ANTI_NETANYAHU ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all ///
	if SECURITY==1, cluster(MONTHLY)
eststo: logit CAP2023 TRUST_GOV ANTI_NETANYAHU interaction2b ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all ///
	if SECURITY==1, cluster(MONTHLY)
esttab using models_Trust2.rtf, se aic bic replace

logit CAP1619 i.TRUST_GOV##i.ANTI_NETANYAHU ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all ///
	if SECURITY == 1, cluster(MONTHLY)
margins ANTI_NETANYAHU, at(TRUST_GOV = (0 1))
marginsplot, ///
	title("") ///
	xtitle("Trust in Government") xlabel(0 "No Trust" 1 "Trust") ///
	ytitle("Probability of Attention to Security") ylabel(0(.2)1) ///
	legend(rows(1)) scheme(s1mono)  plotopts(lwidth(thick))
graph save margins_trust_government.gph, replace

logit CAP2023 i.TRUST_GOV##i.ANTI_NETANYAHU ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all ///
	if SECURITY==1, cluster(MONTHLY)
margins ANTI_NETANYAHU, at(TRUST_GOV=(0 1)) 
marginsplot, ///
	title("") ///
	xtitle("Trust in Government") xlabel(0 "No Trust" 1 "Trust") ///
	ytitle("Probability of Attention to Government and Identity") ylabel(0(.2)1) ///
	legend(rows(1)) scheme(s1mono)  plotopts(lwidth(thick))
graph save margins_trust_security.gph, replace

grc1leg margins_trust_government.gph margins_trust_security.gph, ycommon

graph export "Figure7_Margins_Trust.jpeg", width(4800) height(3600) replace


// trust 4 scale / security crisis 
eststo clear
eststo: logit CAP1619 trust_gov ANTI_NETANYAHU ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all ///
	if SECURITY==1, cluster(MONTHLY)
eststo: logit CAP1619 trust_gov ANTI_NETANYAHU interaction2b4 ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all ///
	if SECURITY==1, cluster(MONTHLY)
eststo: logit CAP2023 trust_gov ANTI_NETANYAHU ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all ///
	if SECURITY==1, cluster(MONTHLY)
eststo: logit CAP2023 trust_gov ANTI_NETANYAHU interaction2b4 ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all ///
	if SECURITY==1, cluster(MONTHLY)
esttab using models_Trust4.rtf, se aic bic replace

logit CAP1619 c.trust_gov##i.ANTI_NETANYAHU ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all ///
	if SECURITY == 1, cluster(MONTHLY)
margins ANTI_NETANYAHU, at(trust_gov = (0 0.33 0.67 1))
marginsplot, ///
	title("") ///
	xtitle("Trust in Government") xlabel(0 "No Trust" 1 "Trust") ///
	ytitle("Probability of Attention to Defense and Foreign Affairs") ylabel(0(.2)1) ///
	legend(rows(1)) scheme(s1mono)
graph save margins_trust_government.gph, replace

logit CAP2023 c.trust_gov##i.ANTI_NETANYAHU ideology ///
	gender jewish ib2.age_group ib2.EDUCATION3 ib2.religiosity_all ///
	if SECURITY==1, cluster(MONTHLY)
margins ANTI_NETANYAHU, at(trust_gov=(0 0.33 0.67 1)) 
marginsplot, ///
	title("") ///
	xtitle("Trust in Government") xlabel(0 "No Trust" 1 "Trust") ///
	ytitle("Probability of Attention to Government and Identity") ylabel(0(.2)1) ///
	legend(rows(1)) scheme(s1mono)
graph save margins_trust_security.gph, replace

grc1leg margins_trust_government.gph margins_trust_security.gph, ycommon
}
	
	
	


